package com.snow.config.amdin;
import com.snow.pojo.model.UserDomain;
import com.snow.service.user.UserService;
import com.snow.utils.TaleUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @Description:
 * @author: snow
 * @Date: 2020-01-21
 **/
public class UserRealm extends AuthorizingRealm {
    @Autowired
    private UserService userService;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken userToken = (UsernamePasswordToken) token;
        UserDomain user = userService.getUserByUsername(userToken.getUsername());
        if(user == null){
            return null;
        }
        HashMap<String, Object> principals = new HashMap<>();
        principals.put("username",user.getUsername());
        principals.put("user",user);
        return new SimpleAuthenticationInfo(principals,user.getPassword(), ByteSource.Util.bytes(user.getSalt()),this.getName());

    }
}
